1359A - Berland Poker - CodeForces Solution


brute force greedy math *1000

Please click on ads to support us..

Python Code:

for i in range(int(input())):
    n, m, k = map(int, input().split())
    d = n // k
    a1 = min(m, d)
    a2 = (m - a1 + k - 2) // (k - 1)
    print(a1 - a2)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ba boolalpha
#define F first
using vi = vector<int>;
using vll = vector<ll>;
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
vector<int> dx = {1, 1, -1, -1};
vector<int> dy = {1, -1, 1, -1};
const long long int mod = 1000000007;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define vect_in(v,n) loop(i,0,n){ll temp; cin>>temp; v.pb(temp);}
#define vect_out(v,n) loop(i,0,n){cout<<v[i]<<" ";}cout<<endl;
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
long long binpow(long long a, long long b, long long m) {  a %= m;long long res = 1; while (b > 0) { if (b & 1)res = res * a % m;a = a * a % m;  b >>= 1;} return res;}
long long sum_array(vector<ll>&ans){ll n = ans.size();ll x = 0;loop(i,0,n){x+=ans[i];}return x;}
ll phi(ll n){ll res = n;for(int i = 2;i*i<=n;i++){if(n%i==0){while(n%i==0){n=n/i;}res-=res/i;}}if(n>1)res=res-res/n;return res;}
ll extendedgcd(ll a, ll b, ll& x, ll& y){if(b==0){x=1;y=0;return a;}ll x1,y1; ll d = extendedgcd(b,a%b,x1,y1);x=y1;y=x1-y1*(a/b);return d;}
bool lineardiph(ll a,ll b,ll c,ll&x,ll&y,ll&g){g=extendedgcd(abs(a),abs(b),x,y); if(c%g!=0){return false;}; x*=c/g;y*=c/g;if(a<0){x=-x;}if(b<0){y=-y;}return true;}

void solve(){
ll n,m,k;
cin>>n>>m>>k;

bool flag=1,flag2=1;

if(n%k==0)
{
    flag=0;
}

if(((m-((n/k)+flag))%(k-1))==0)
{
    flag2=0;
}

if(((n/k)+flag)>=m)
{
    cout<<m<<endl;
    return;
}

else
{
    cout<<(((n/k)+flag)-(((m-((n/k)+flag))/(k-1))+flag2))<<endl;
}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t = 1;
    cin>>t;
    
    while(t--){
        solve();
    }
 
}


Comments

Submit
1 Comments
  • 4/11/2023 23:41 - Asia/Dhaka

#include<stdio.h>
int main()
{
int n,m,k,t;
scanf("%d",&t);
for(int i = 1 ; i <= t ; i++)
{int v;
int x;
  scanf("%d%d%d",&n,&m,&k);
  int p = n/k;
  int left = k-1;

  if(p >= m)
  {
      printf("%d\n",m);
  }
  else if(p < m)
    {  v = m - p;
     x = v;
     int num[left];


      for(int j = 0 ; j < left ; j++)
      {
         num[j] = v/left;
         v  -= num[j];
      }
      if(left > 1)
      {for(int j = 0 ; j < left ; j++)
      {
          num[j]++;
      }}

      int out = p - num[0];
      printf("%d\n",out);

    }

 

}

return 0 ;

 

}


More Questions

Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character